package com.emagic.manage.modules.loginmodule.presenter;

import android.text.TextUtils;

import com.emagic.manage.biz.LoginCallback;
import com.emagic.manage.data.entities.response.UserResponse;
import com.emagic.manage.data.exception.BizException;
import com.emagic.manage.modules.loginmodule.domain.LoginUseCase;
import com.emagic.manage.modules.loginmodule.view.LoginView;
import com.emagic.manage.mvp.presenters.Presenter;
import com.emagic.manage.mvp.views.InterfaceView;
import com.fernandocejas.frodo.annotation.RxLogSubscriber;
import com.xitaiinfo.library.injections.ActivityScope;
import com.xitaiinfo.library.utils.CommonUtils;
import com.xitaiinfo.library.utils.EncryptUtils;

import javax.inject.Inject;

import rx.Subscriber;

/**
 * TODO 描述该文件做什么
 *
 * @author baoy
 * @version 1.0
 *          Create by 16/4/26 下午3:29
 */
@ActivityScope
public class LoginPresenter extends LoginCallback implements Presenter {

    private LoginView view;
    private LoginUseCase mLoginUseCase;

    @Inject
    public LoginPresenter(LoginUseCase loginUseCase) {
        this.mLoginUseCase = loginUseCase;
    }

    @Override
    public void onResume() {
    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        mLoginUseCase.unSubscribe();
    }

    @Override
    public void attachView(InterfaceView v) {
        this.view = (LoginView) v;
    }

    /**
     * 登录
     */
    public void login(String nickname, String pwd) {

        view.showProgress();
        mLoginUseCase.setCidentifier(CommonUtils.getUUID(view.getContext()));
        mLoginUseCase.setPhone(nickname);
        mLoginUseCase.setPwd(EncryptUtils.md5(EncryptUtils.md5(pwd).concat(EncryptUtils.md5(nickname))));
        mLoginUseCase.setAlias("1");
        mLoginUseCase.execute(new LoginSubscriber());
    }

    /**
     * 登录成功后处理
     */
    private void onLoginSuccess(UserResponse userResponse) {

        handleLoginSuccess(view.getContext(), userResponse);

        //处理界面跳转
        view.render();
    }

    @RxLogSubscriber
    private class LoginSubscriber extends Subscriber<UserResponse> {

        @Override
        public void onCompleted() {
            view.hideProgress();
        }

        @Override
        public void onError(Throwable e) {
            view.hideProgress();
            if (e instanceof BizException) {
                String code = ((BizException) e).code();
                if (code.equals("-2")) {
                    view.showError("您的账号在其他设备登录");
                } else if (code.equals("1")) {
                    view.showError("帐号未激活");
                } else if (code.equals("2")) {
                    view.showError("帐号已冻结");
                } else if (code.equals("5")) {
                    view.showError("密码错误");
                } else if (code.equals("6")) {
                    view.showError("帐号不存在");
                } else {
                    view.showError("服务端异常");
                }
            } else {
                view.showError(e.getMessage());
            }

        }

        @Override
        public void onNext(UserResponse userResponse) {
            if (userResponse != null && !TextUtils.isEmpty(userResponse.getResult())) {
                LoginPresenter.this.onLoginSuccess(userResponse);
            }

        }
    }
}
